<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>XMLHttpRequest_数据提交</title>
</head>

<body>
  <button class="reg-btn">注册用户</button>
  <script>
    /**
     * 目标：使用xhr进行数据提交-完成注册功能
    */
    document.querySelector('.reg-btn').addEventListener('click', () => {
      const xhr = new XMLHttpRequest()
      xhr.open('post', 'http://hmajax.itheima.net/api/register')
      xhr.addEventListener('loadend', () => {
        console.log(xhr.response)
        
      })
      //设置请求头
      xhr.setRequestHeader('Content-Type', 'application/json')
      //准备提交的数据
      const userObj = {
        username: 'itheima008', 
        password: '7654321'
      }
      //转为字符串
      const userStr = JSON.stringify(userObj)
      //发送请求体数据
      xhr.send(userStr)
    })
  </script>
</body>

</html>